#include "stop.h"
#include "ui_stop.h"
#include "tomatoclock.h"
#include <QTimer>
#include "achieve.h"
#include "endform.h"

Stop *st;

Stop::Stop(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Stop)
{
    ui->setupUi(this);
    st=this;
    initForm();
    connect(m_timer,&QTimer::timeout,this,&Stop::timeEnd);
}

Stop::~Stop()
{
    delete ui;
}

void Stop::on_pushButton_clicked()
{   ui->pushButton_2->setText("继续");
    m_timer->stop();
    ui->label->setTime(m_time);
    ui->label->csetTime(cm_time);
    m_time --;
}

void Stop::on_pushButton_2_clicked()
{   ui->pushButton_2->setText("开始");
    //每10毫秒执行一次
    //每10毫秒刷新一次为了让进度条更流畅
    m_timer->start(10);
}

void Stop::timerEvent(QTimerEvent *event)
{
}
void Stop::setorigin(int l)
{
    origin=l;
}
void Stop::initForm()
{
    //设置背景色，qss
    QString strStyle = "QWidget#TimerWidget{background-color:rgb(68,85,123);}";
    setStyleSheet(strStyle);
    setFixedSize(800,600);
    ui->label->setFixedSize(250,250);
    ui->pushButton_2->setText("开始");
    m_timer = new QTimer(this);
    ui->label->setTime(m_time);
    ui->label->csetTime(cm_time);
}
void Stop::timeEnd()
{
    //如果小于0停止计时
    if(m_time < 0&&ui->project_1->text()!="休息")
    {    endform*end=new endform;
        qDebug()<<"show origin in end"<<origin;
        Achieve *true_achieve=new Achieve;
        true_achieve->gainfruit(origin,true);
        end->show();
        m_time=29900;
        cm_time=29900;
        ui->project_1->setText("休息");
        if(m_time == 0)
        {
            m_timer->stop();
        }
        return;
    }
    if(m_time < 0&&ui->project_1->text()=="休息")
    {
        m_timer->stop();
    }
    ui->label->setTime(m_time);
    ui->label->csetTime(cm_time);
    m_time --;
}
void Stop::rest()
{
    ui->label->setTime(29900);
    ui->project_1->setText("休息");
    this->close();
}

void Stop::on_pushButton_3_clicked()
{
    this->close();
}

